﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace Spencen.Mobile.UI.Easings
{
    public class ElasticEasing : IEasingFunction
    {
        public ElasticEasing()
        {
            Amplitude = 1.0f;
            Period = 0.45f;
        }

        public float Amplitude { get; set; }
        public float Period { get; set; }

        #region IEasingFunction Members

        public float EasedValue( float actualValue )
        {
            return 1.0f - Ease( 1.0f - actualValue );
        }

        private float Ease( float actualValue )
        {
            float s;
            if ( actualValue < 0 || actualValue > 1 ) return actualValue;
            if ( Amplitude < 1 )
            {
                Amplitude = 1;
                s = Period / 4;
            }
            else
            {
                s = (float)( Period / ( 2 * Math.PI ) * Math.Asin( 1 / Amplitude ) );
            }
            var result = -( Amplitude * Math.Pow( 2, 10 * ( actualValue -= 1 ) ) * Math.Sin( ( actualValue - s ) * ( 2 * Math.PI ) / Period ) );
            return (float)result;
        }

        #endregion
    }
}
